package com.search.ThreadService;

import com.search.commons.SearchCore;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * Created by yijy on 2017/7/6.
 */
@Service
public class GrabThread {

    private CountDownLatch count;

    private Map<String,String> downloadUrl;

    public void setCount(CountDownLatch count) {
        this.count = count;
    }

    public void setDownloadUrl(Map<String, String> downloadUrl) {
        this.downloadUrl = downloadUrl;
    }

    @Async
    public void executeAsyncTaskRun(String url,String title)
    {
        try {
            Document document = SearchCore.getHtmlDom(url);

            Element element = document.body();

            Elements elements = element.getElementsByClass("BotInfo");

            if(elements.size() > 0)
            {
                Elements valueDoms =  elements.get(0).getElementsByTag("a");
                if(valueDoms.size() > 0)
                {
                    downloadUrl.put(title,valueDoms.get(0).html());
                }
            }

        }
        catch (Exception e)
        {

        }finally {
            //计数器减一
            count.countDown();
        }
    }
}
